package com.hui.auth.config.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.util.DigestUtils;

/**
 * 检验密码
 * Created by LiaoYunHui on 2017/9/29/029.
 */
public class CredentialsMatcher extends SimpleCredentialsMatcher {
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken utoken = (UsernamePasswordToken) token;
        //获得用户输入的密码:(可以采用加盐(salt)的方式去检验)，此处采用spring的md5加密
        String inPassword = new String(utoken.getPassword());
        inPassword = DigestUtils.md5DigestAsHex(inPassword.getBytes());
        //获得数据库中的密码
        String dbPassword = (String) info.getCredentials();
        //进行密码的比对
        return this.equals(inPassword, dbPassword);
    }

//    public static void main(String[] args) {
//        String inPassword = "rst2017@!#68";
//        System.out.println(DigestUtils.md5DigestAsHex(inPassword.getBytes()));
//    }
}
